package com.example.noobpackage.ui.packagemarket

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.recyclerview.widget.RecyclerView
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.MarketPackageData


class PackageMarketAdapter :
    RecyclerView.Adapter<PackageMarketAdapter.ViewHolder>() {
    lateinit var myItemClickListener: (Int, FragmentNavigator.Extras) -> Unit
    private var packagesList: List<MarketPackageData> = listOf()

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val takingPlace = view.findViewById<TextView>(R.id.takingPlace)
        val takingToPlace = view.findViewById<TextView>(R.id.takingToPlace)
        val remarks = view.findViewById<TextView>(R.id.remarks)
        val weight = view.findViewById<TextView>(R.id.weight)
        var packageMarketItemCard = view.findViewById<View>(R.id.packageMarketItemCard)

        val takingPlaceIcon = view.findViewById<ImageView>(R.id.takingPlaceIcon)
        val takingToPlaceIcon = view.findViewById<ImageView>(R.id.takingToPlaceIcon)
        val weightIcon = view.findViewById<ImageView>(R.id.weightIcon)
        val remarksIcon = view.findViewById<ImageView>(R.id.remarksIcon)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.package_market_item_card, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val packageData = packagesList.elementAt(position)
        //绑定数据
        holder.takingPlace.text = packageData.expressLocation
        holder.takingToPlace.text = packageData.expressToLocation
        holder.weight.text = packageData.expressWeight
        holder.remarks.text = packageData.expressRemarks

        //绑定列表项，实现共享元素动画
        holder.takingPlace.transitionName = "takingPlace$position"
        holder.takingToPlace.transitionName = "takingToPlace$position"
        holder.weight.transitionName = "weight$position"
        holder.remarks.transitionName = "remarks$position"
        holder.takingPlaceIcon.transitionName = "takingPlaceIcon$position"
        holder.takingToPlaceIcon.transitionName = "takingToPlaceIcon$position"
        holder.weightIcon.transitionName = "weightIcon$position"
        holder.remarksIcon.transitionName = "remarksIcon$position"

        holder.packageMarketItemCard.setOnClickListener {
            // 创建extras，让外部接口进行跳转
            val extras = FragmentNavigatorExtras(
                holder.takingPlace to "takingPlace$position",
                holder.takingToPlace to "takingToPlace$position",
                holder.weight to "weight$position",
                holder.remarks to "remarks$position",
                holder.takingPlaceIcon to "takingPlaceIcon$position",
                holder.takingToPlaceIcon to "takingToPlaceIcon$position",
                holder.weightIcon to "weightIcon$position",
                holder.remarksIcon to "remarksIcon$position"
            )
            myItemClickListener(position, extras)
        }
    }

    override fun getItemCount(): Int {
        return packagesList.size
    }

    fun setItemClick(itemClickListener: (Int, FragmentNavigator.Extras) -> Unit) {
        myItemClickListener = itemClickListener
    }

    fun setMarketList(newList: List<MarketPackageData>) {
        packagesList = newList
        notifyDataSetChanged()
    }
}